package com.vict.framework.pay.taskimpl;

import com.vict.framework.pay.bean.PayTaskDTO;
import com.vict.framework.pay.service.FramworkPayInnerService;
import com.vict.framework.task.annotation.TaskConsumerConfiguration;
import com.vict.framework.task.bean.dto.Task;
import com.vict.framework.task.bean.dto.TaskConsumeResult;
import com.vict.framework.task.core.TaskExecute;
import com.vict.framework.utils.lock.LockApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@TaskConsumerConfiguration(type = PayTask.type, sameTimeWorkNum = 20,
    failRetryTimeSeconds={
        1*60, 2*60, 5*60, 10*60
    })
public class PayTask implements TaskExecute<PayTaskDTO> {
    public static final String type = "PayTask";

    @Autowired
    private FramworkPayInnerService framworkPayInnerService;

    @Override
    public TaskConsumeResult doConsume(Task task, PayTaskDTO payTaskDTO) {

        String payDtoId = payTaskDTO.getPayDtoId();
        LockApi lock = LockApi.getLockOverThrowException("payServer_payDtoId-" + payDtoId, 40L);
        try{
            lock.lockBlock();

            framworkPayInnerService.payFinish(payTaskDTO);

        }finally{
            lock.unlockIfSuccess();
        }

        return task.commitConsumer();
    }
}
